package ig.metrica.ejb.conflujos.dao;


import ig.da.abs.AbstractBasicDao;
import java.sql.SQLException;
import java.lang.Exception;
import ig.metrica.ejb.conflujos.ConfiguracionflujosDTO;
import ig.metrica.ejb.conflujos.ConfiguracionflujosPK;

/**
 * GenJ 2011
 * @author usuario
 *
 * Is an Class for table DB for DAO
 */
public class ConfiguracionflujosDAO extends AbstractBasicDao{

	// -----------------------------------------------------------------
	// Constants
	// -----------------------------------------------------------------

	// -----------------------------------------------------------------
	// Attributes
	// -----------------------------------------------------------------
    private java.sql.Connection connection = null;

	// -----------------------------------------------------------------
	// Constructors
	// -----------------------------------------------------------------
    public ConfiguracionflujosDAO ()  {

    }

	// -----------------------------------------------------------------
	// All Methods
	// -----------------------------------------------------------------

                                                                

    /**
     * Method for create an record of table of table
     * @return : the pk of table
     * @param dto
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public  ig.metrica.ejb.conflujos.ConfiguracionflujosPK  create(ConfiguracionflujosDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;

        try{
            String QUERY = "INSERT INTO CONFIGURACIONFLUJOS ( IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL ) VALUES ( ?,  ?,  ?,  ?,  ?,  ? )";

            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getIdmodelo());
            prep.setInt (2,dto.getAltura());
            prep.setInt (3,dto.getDuracion());
            prep.setInt (4,dto.getProcentaje());
            prep.setInt (5,dto.getValormora());
            prep.setInt (6,dto.getSaldocapital());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return dto.getPk();
    }


    /**
     * Update by Pk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int update (ConfiguracionflujosDTO dto) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE CONFIGURACIONFLUJOS SET PROCENTAJE = ?, VALORMORA = ?, SALDOCAPITAL = ?, , WHERE IDMODELO = ? AND ALTURA = ? AND DURACION = ?";
            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getProcentaje());
            prep.setInt (2,dto.getValormora());
            prep.setInt (3,dto.getSaldocapital());

            prep.setInt (4,dto.getIdmodelo());
            prep.setInt (5,dto.getAltura());
            prep.setInt (6,dto.getDuracion());
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }


    public int deleteByModelo( Integer idModelo ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "DELETE FROM CONFIGURACIONFLUJOS WHERE IDMODELO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,idModelo);
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }    	
    }
    /**
     * Delete by Pk
     * @param pk
     * @return the number record delete
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int delete (ConfiguracionflujosPK pk ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM CONFIGURACIONFLUJOS WHERE IDMODELO = ? AND ALTURA = ? AND DURACION = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,pk .getIdmodelo());
            prep.setInt (2,pk .getAltura());
            prep.setInt (3,pk .getDuracion());
            
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Find by Pk
     * @param pk
     * @return DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public ConfiguracionflujosDTO findByPk (ConfiguracionflujosPK pk ) throws SQLException, Exception, Throwable {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        ConfiguracionflujosDTO configuracionflujos = null;
        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS WHERE IDMODELO = ? AND ALTURA = ? AND DURACION = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,pk .getIdmodelo());
            prep.setInt (2,pk .getAltura());
            prep.setInt (3,pk .getDuracion());
            res = prep.executeQuery();
            if (res.next()) {
                configuracionflujos = new ConfiguracionflujosDTO(  res.getInt ( 1 ), res.getInt ( 2 ), res.getInt ( 3 ), res.getInt ( 4 ), res.getInt ( 5 ), res.getInt ( 6 ));
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,res,connection);
        }
        return configuracionflujos;
    }


    /**
     * The List By idmodelo
     * @param idmodelo
     * @return list of ConfiguracionflujosDTO
     */
    public java.util.List<ConfiguracionflujosDTO> findByIdmodelo (Integer  idmodelo) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionflujosDTO> list=new java.util.ArrayList<ConfiguracionflujosDTO>();

        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS WHERE IDMODELO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,idmodelo);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionflujosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 ), (Integer)res.getObject ( 5 ), (Integer)res.getObject ( 6 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By altura
     * @param altura
     * @return list of ConfiguracionflujosDTO
     */
    public java.util.List<ConfiguracionflujosDTO> findByAltura (Integer  altura) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionflujosDTO> list=new java.util.ArrayList<ConfiguracionflujosDTO>();

        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS WHERE ALTURA = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,altura);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionflujosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 ), (Integer)res.getObject ( 5 ), (Integer)res.getObject ( 6 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By duracion
     * @param duracion
     * @return list of ConfiguracionflujosDTO
     */
    public java.util.List<ConfiguracionflujosDTO> findByDuracion (Integer  duracion) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionflujosDTO> list=new java.util.ArrayList<ConfiguracionflujosDTO>();

        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS WHERE DURACION = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,duracion);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionflujosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 ), (Integer)res.getObject ( 5 ), (Integer)res.getObject ( 6 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By procentaje
     * @param procentaje
     * @return list of ConfiguracionflujosDTO
     */
    public java.util.List<ConfiguracionflujosDTO> findByProcentaje (Integer  procentaje) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionflujosDTO> list=new java.util.ArrayList<ConfiguracionflujosDTO>();

        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS WHERE PROCENTAJE = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,procentaje);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionflujosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 ), (Integer)res.getObject ( 5 ), (Integer)res.getObject ( 6 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By valormora
     * @param valormora
     * @return list of ConfiguracionflujosDTO
     */
    public java.util.List<ConfiguracionflujosDTO> findByValormora (Integer  valormora) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionflujosDTO> list=new java.util.ArrayList<ConfiguracionflujosDTO>();

        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS WHERE VALORMORA = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,valormora);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionflujosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 ), (Integer)res.getObject ( 5 ), (Integer)res.getObject ( 6 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * The List By saldocapital
     * @param saldocapital
     * @return list of ConfiguracionflujosDTO
     */
    public java.util.List<ConfiguracionflujosDTO> findBySaldocapital (Integer  saldocapital) throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionflujosDTO> list=new java.util.ArrayList<ConfiguracionflujosDTO>();

        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS WHERE SALDOCAPITAL = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,saldocapital);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionflujosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 ), (Integer)res.getObject ( 5 ), (Integer)res.getObject ( 6 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }


    /**
     * The List By saldocapital
     * @param saldocapital
     * @return list of ConfiguracionflujosDTO
     */
    public java.util.List<ConfiguracionflujosDTO> findAll() throws SQLException, Exception, Throwable {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<ConfiguracionflujosDTO> list=new java.util.ArrayList<ConfiguracionflujosDTO>();

        try{
            String QUERY = "SELECT IDMODELO, ALTURA, DURACION, PROCENTAJE, VALORMORA, SALDOCAPITAL FROM CONFIGURACIONFLUJOS";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new ConfiguracionflujosDTO(  (Integer)res.getObject ( 1 ), (Integer)res.getObject ( 2 ), (Integer)res.getObject ( 3 ), (Integer)res.getObject ( 4 ), (Integer)res.getObject ( 5 ), (Integer)res.getObject ( 6 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }



    /**
     * Open the connection
     * @return
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private java.sql.Connection open() throws ClassNotFoundException, java.sql.SQLException,Exception {
    	return getConnection("METRICADW");
    }



}
